package com.qishanor.wpan.controller;

import cn.hutool.core.lang.Console;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qishanor.framework.util.R;
import com.qishanor.wpan.model.WpanFile;
import com.qishanor.wpan.service.WpanFileService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.sql.Wrapper;

/**
 * @description:
 * @author: 周振林
 * @date: 2023-05-24
 * @website: http://www.zhouzhenlin.com - 沉淀、分享、成长、让自己和他人都有所收获
 **/

@RestController
@RequestMapping("/api/wpan")
@AllArgsConstructor
public class WpanFileController {

    private final WpanFileService wpanFileService;

    @GetMapping("/list")
    public R list(Page page,String filename, String type){

       IPage result=wpanFileService.page(page,Wrappers.<WpanFile>lambdaQuery()
                .eq(ObjectUtil.isNotEmpty(filename),WpanFile::getRealName,filename)
                .or().eq(ObjectUtil.isNotEmpty(type),WpanFile::getCategory,type));

        return R.ok(result);
    }


    /**
     * 上传文件 文件名采用uuid,避免原始文件名中带"-"符号导致下载的时候解析出现异常
     * @param file 资源
     * @return R(/ admin / bucketName / filename)
     */
    @PostMapping(value = "/uploadFile")
    public R uploadFile(@RequestPart("file") MultipartFile file) {


        return  wpanFileService.uploadFile(file);
    }

}
